<?php


namespace App\Task;

use App\Service\PriceDailyService;
use App\Service\PriceHourlyService;
use App\Service\PriceMonthlyService;
use App\Service\PriceWeekService;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Di\Annotation\Inject;

#[Crontab(name: "Foo", rule: "*/5 * * * * *", callback: "execute", memo: "这是一个示例的定时任务")]
class KeyLineTask
{
    #[Inject]
    private StdoutLoggerInterface $logger;
    #[Inject]
    private PriceHourlyService $priceHourlyService;
    #[Inject]
    private PriceDailyService $priceDailyService;
    #[Inject]
    private PriceWeekService $priceWeeklyService;
    #[Inject]
    private PriceMonthlyService $priceMonthlyService;

    public function execute()
    {
        //*/5 * * * * * 5秒
        $this->logger->info(date('Y-m-d H:i:s', time()));
        echo "\n", date('Y-m-d H:i:s', time());
    }

    #[Crontab(rule: "*/1 * * * *", memo: "hourly")]
    public function hourly()
    {
        $rs = $rs = $this->priceHourlyService->handle(date('Y-m-d H:00'));
        $info = '|exec hourly' . var_export($rs, true);
        $this->logger->info($info);
        if ($rs) {
            $rs = $this->priceDailyService->handle(date('Y-m-d'));
            $info .= '|exec daily' . var_export($rs, true);
            $this->logger->info($info);

            $rs = $this->priceWeeklyService->handle(date('Y-m-d'));
            $info .= '|exec week' . var_export($rs, true);
            $this->logger->info($info);
            $rs = $this->priceMonthlyService->handle(date('Y-m-d'));
            $info .= '|exec month' . var_export($rs, true);
            $this->logger->info($info);
        }

        if ($rs) {

        }

    }
}
